package com.tanhua.server.service;

import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author 王加鑫
 * @Date 2021/09/03 19:26
 * @Since JDK 1.8
 */
@Service
public class TodayBestService {

    @Autowired
    private RecommendUserService recommendUserService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询今日佳人
     * @return
     */
    public TodayBest queryTodayBest() {
        //从token中获取user对象
        User user = UserThreadLocal.get();
        //根据userId去mongodb中查询佳人一部分数据 recommendUser
        RecommendUser recommendUser = recommendUserService.queryRecommendUser(user.getId());
        if(null == recommendUser){
            //该用户如果在mangodb中查询不到数据，给他一个默认的佳人数据缘分值
            recommendUser = new RecommendUser();
            recommendUser.setScore(80.0);
        }
        //根据userId去mysql中查询佳人一部分数据
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(user.getId());
        if(null == userInfo){
            //该用户如果在mangodb中查询不到数据，给他一个默认的佳人数据
            userInfo = new UserInfo();
            userInfo.setId(defaultUser);
            userInfo.setAge(18);
            userInfo.setTags("单身,本科,年龄相仿");
            userInfo.setNickName("黑马小妹");
            userInfo.setLogo("http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/21.jpg");
            userInfo.setSex(SexEnum.WOMAN);
        }
        TodayBest todayBest = new TodayBest();
        todayBest.setFateValue(Double.valueOf(Math.floor(recommendUser.getScore())).longValue());
        todayBest.setId(userInfo.getId());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
        todayBest.setAge(userInfo.getAge());
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man":"woman");
        return todayBest;
    }
}
